/*****************************************************************************************
 * Dexter : Distributed Experimental and Tester Platform
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *****************************************************************************************/
package dexter.core.host;

import java.io.Serializable;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.UnknownHostException;


public class HostAddress implements Serializable{

	private static final long serialVersionUID = 1257900524775363954L;

	private InetSocketAddress isa; 
	
	public HostAddress(String host, int port) {
	  try {
		this.isa = new InetSocketAddress(InetAddress.getByName(host), port);

	} catch (UnknownHostException e) {
		e.printStackTrace();
	}
	}

	public HostAddress(SocketAddress sa) {
		
		this.isa = (InetSocketAddress) sa;
	}

	public InetSocketAddress getAddress() {
		
		return this.isa;
	}
	
	public String toString() {
		
		return "<"+getAddress().getHostName()+":"+getAddress().getPort()+">";
	}
	
	public boolean equals(Object obj) {
		boolean eq = false;
		
		if (obj instanceof HostAddress) {
			HostAddress other = (HostAddress) obj;
			return isa.equals(other.getAddress());
		}
		return eq;
	}
	
	public boolean isAlive() {
		String host = getAddress().getHostName();
		try {
		  return InetAddress.getByName(host).isReachable(3000);
		} catch(Exception e) {
			return false;
		}
	}

}
